<?php

namespace App\Admin\Controllers;

use App\Models\Config;

use App\Models\Menu;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ConfigController extends BaseController
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {

        return Admin::content(function (Content $content) {
            if ($menuId = request('menuId')) {
                $menu = Menu::find($menuId);
                session(['menu' => $menu]);
            } else {
                session(['menu' => null]);
                $menu=null;
            }
            $content->header(empty($menu) ? '配置' : $menu->title.'配置');
            $content->description(empty($menu) ? '配置' : $menu->title.'配置');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Config::class, function (Grid $grid) {
            if ($menu = session('menu')) {
                $grid->model()->where('menu_id', $menu->id);
                $grid->disableActions();
                $grid->disableCreation();
                $grid->disableExport();
                $grid->disableFilter();
                $grid->disablePagination();
                $grid->disableRowSelector();
            }else{
                $grid->id();
            }
            $grid->column('remark', '名称');
            $grid->column('value', '值')->editable('textarea');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Config::class, function (Form $form) {

            if('editor'==request('type')){
                $form->editor('value', '值')->rules('required');
            }else{
                $form->text('name', '健')->rules('required');
                $form->text('remark', '名称')->rules('required');
                $form->text('value', '值')->rules('required');
                $form->select('menu_id','模块')->options(function ($ids) {
                $data = Menu::find($ids);
                    return $data ? $data->pluck('title', 'id') : null;
                })->ajax('/admin/menu/get')->rules('required');
            }
            $form->saved(function (Form $form) {
                return redirect('/admin');
            });
        });
    }
}
